import express from 'express';
import { expressjwt } from 'express-jwt';

import { SYSTEM } from '../config/Config';

/**
 * 不需要鉴权的地址
 */
export const lessPaths = ["/register", "/register/captcha", "/login", "/login/captcha", "/public/"];

export default expressjwt({
    secret: SYSTEM.privateKey,
    algorithms: ['HS256'],
    getToken: (req: express.Request): string | Promise<string> | undefined => {
        if (
            req.headers.authorization &&
            req.headers.authorization.split(" ")[0] === "Bearer"
        ) {
            return req.headers.authorization.split(" ")[1];
        } else if (req.query && req.query.token) {
            return <string>req.query.token;
        }
        return undefined;
    }
}).unless({ path: lessPaths });
